#include "currencydelegate.h"

#include "cashplusplus.h"

CurrencyDelegate::CurrencyDelegate(QObject* parent)
	: ReadOnlyCurrencyDelegate(parent)
{
}

QWidget* CurrencyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem&, const QModelIndex&) const
{
	QDoubleSpinBox* spin = new QDoubleSpinBox(parent);
	spin->setMinimum(-CashPlusPlus::MAX_MONEY_MAGNITUDE);
	spin->setMaximum(CashPlusPlus::MAX_MONEY_MAGNITUDE);
	spin->setPrefix("$");

	return spin;
}

void CurrencyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
	double value = index.model()->data(index, Qt::EditRole).toDouble();

	QDoubleSpinBox *spin = static_cast<QDoubleSpinBox*>(editor);
	spin->setValue(value);
}

void CurrencyDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
	QDoubleSpinBox *spin = static_cast<QDoubleSpinBox*>(editor);
	spin->interpretText();

	model->setData(index, spin->value(), Qt::EditRole);
}

void CurrencyDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex&) const
{
	editor->setGeometry(option.rect);
}
